//
//  ReadController.m
//  News
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "ReadController.h"
#import "ReadDataHelper.h"
#import "ReadModel.h"
#import "ReadOneImageCell.h"
#import "ReadThreeImageCell.h"
#import "MJRefresh.h"
#import "ReadDetailViewController.h"

@interface ReadController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ReadController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.navigationController.hidesBarsOnSwipe = YES;
    self.navigationItem.title = @"阅读";
    [self.navigationController.navigationBar setBackgroundColor:[UIColor orangeColor]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 请工具类请求数据
    [[ReadDataHelper sharedReadDataHelper] requestDataFromNetWorkWithUrl:kDragDownUrls_read didFinished:^{
        self.dataArray = [ReadDataHelper sharedReadDataHelper].readDataArray;
        [self.tableView reloadData];
    }];
    
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [[ReadDataHelper sharedReadDataHelper] requestDataFromNetWorkWithUrl:kDragDownUrls_read didFinished:^{
            self.dataArray = [ReadDataHelper sharedReadDataHelper].readDataArray;
            [self.tableView reloadData];
        }];
        // 结束刷新
        [self.tableView.header endRefreshing];
        
    }];
    
    
    // 上拉刷新
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [[ReadDataHelper sharedReadDataHelper] requestDataFromNetWorkWithUrl:kDragUpUrls_read didFinished:^{
            self.dataArray = [ReadDataHelper sharedReadDataHelper].readDataArray;
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }];
        
    }];
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReadModel *model = self.dataArray[indexPath.row];
//    if (model.imgnewextra) {
//        ReadThreeImageCell *cell = [ReadThreeImageCell cellWithTableView:tableView];
//        cell.model = model;
//        return cell;
//    } else {
        ReadOneImageCell *cell = [ReadOneImageCell cellWithTableView:tableView];
        cell.model = model;
        return cell;
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    ReadModel *model = self.dataArray[indexPath.row];
//    if (model.imgnewextra) {
//        return 250;
//    } else {
        return 130;
//    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadDetailViewController *detailVC = [[ReadDetailViewController alloc] init];
    ReadModel *model = self.dataArray[indexPath.row];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
